<?php

namespace common\models\teacher;

use Yii;
use common\helpers\ArrayHelper;
use common\helpers\Url;
use common\helpers\StringHelper;
use common\models\user\UserFollow;

/**
 * This is the model class for table "{{%teacher}}".
 *
 * @property int $id
 * @property int $user_id 用户ID
 * @property string $number 教师编号
 * @property string $name 姓名
 * @property string $avatar 头像
 * @property string $mobile 手机号
 * @property int $gender 性别:0未知，1男，2女
 * @property int $role_id 角色ID
 * @property int $nation_id 民族ID
 * @property string $birthday 出生日期
 * @property int $grade_id 教师认证等级ID
 * @property int $status 状态
 * @property int $created_at 创建时间
 * @property int $updated_at 更新时间
 * @property int $deleted_at 删除时间
 */
class Teacher extends \common\base\ActiveRecord {

    use \common\traits\Constants;
    use \common\traits\TimestampBehavior;
    use \common\traits\QueryInstanceActiveRecord;

    const STATUS_ACTIVE = 10;
    const STATUS_DEFAULT = 0;
    //
    const ROLE_ID_TEACHER = 1;
    const ROLE_ID_PRESIDENT = 3;

    /**
     * {@inheritdoc}
     */
    public static function tableName() {
        return '{{%teacher}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules() {
        return [
            [['number', 'name', 'mobile'], 'required'],
            [['user_id', 'gender', 'role_id', 'nation_id', 'grade_id', 'status', 'created_at', 'updated_at', 'deleted_at'], 'integer'],
            [['birthday'], 'safe'],
            [['number'], 'string', 'max' => 32],
            [['name'], 'string', 'max' => 50],
            [['avatar', 'description'], 'string', 'max' => 255],
            [['mobile'], 'string', 'max' => 11],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'user_id' => '用户UID',
            'number' => '教师编号',
            'name' => '姓名',
            'description' => '简介',
            'avatar' => '头像',
            'mobile' => '手机号',
            'gender' => '性别',
            'role_id' => '角色ID',
            'nation_id' => '民族ID',
            'birthday' => '出生日期',
            'grade_id' => '认证等级',
            'status' => '状态',
            'created_at' => '创建时间',
            'updated_at' => '更新时间',
            'deleted_at' => '删除时间',
            //
            'scopes' => '教学分类',
        ];
    }

    public function getRole() {
        return $this->hasOne(TeacherRole::className(), ['id' => 'role_id']);
    }

    public function getGrade() {
        return $this->hasOne(TeacherGrade::className(), ['id' => 'grade_id']);
    }

    public function getCerts() {
        return $this->hasMany(TeacherCertificate::className(), ['teacher_id' => 'id']);
    }

    public function getCertsImage() {
        if (!empty($this->certs)) {
            return ArrayHelper::getColumn($this->certs, 'picture');
        }
        return [];
    }

    public function getTeachScopes() {
        return $this->hasMany(\common\models\teach\TeachScope::className(), ['teacher_id' => 'id']);
    }

    public function getCategorys() {
        return $this->hasMany(\common\models\course\CourseCategory::className(), ['id' => 'course_category_id'])
        ->via('teachScopes');
    }

    public function getScopes() {
        return ArrayHelper::getColumn($this->categorys, 'name');
    }

    public function getNation() {
        return $this->hasOne(\common\models\base\BaseNation::className(), ['id' => 'nation_id']);
    }

    public function getFollow() {
        return $this->hasOne(UserFollow::className(), ['model_id' => 'id'])->andWhere([
            'model' => UserFollow::MODEL_TEACHER,
        ]);
    }

    public function getFollowed() {
        return $this->getFollow()->andWhere(['user_id' => Yii::$app->user->id])->exists();
    }

    public function fields() {
        $fields = parent::fields();
        $fields['avatar'] = function () {
            return Url::toRemote($this->avatar);
        };
        $fields['mobile'] = function () {
            return StringHelper::maskChar($this->mobile, 3, 2);
        };
        $fields['grade'] = function () {
            return $this->grade_id;
        };
        $fields['followed'] = function () {
            return $this->followed;
        };
        return $fields;
    }

}
